//
//  SingerListViewController.m
//  MMLOOK
//
//  Created by BraveMatch on 15/10/7.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "SingerListViewController.h"
#import "SingerListTableViewCell.h"
#import "AsynConnect.h"
#import "MyMusicModel.h"
#import "SongListViewController.h"
#import "JudgeConnection.h"
#import "MBProgressHUD.h"
#define OffWidth [UIScreen mainScreen].bounds.size.width / 375
#define OffHeight [UIScreen mainScreen].bounds.size.height / 667
@interface SingerListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArr;

@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation SingerListViewController
- (void)dealloc
{
    [_table release];
    [_tableArr release];
    [_hud release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArr = [NSMutableArray array];
        
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"歌手列表";
    [self createTableView];
    [self startConnection];
}


-(void)createTableView
{
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];
    image.frame = self.view.frame;
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64*OffHeight) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor = [UIColor clearColor];
    [image addSubview:_table];
    

    [_table release];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"singerList";
    SingerListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[SingerListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    MyMusicModel *model = [_tableArr objectAtIndex:indexPath.row];
    
    [cell setValueWith:model];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70*OffHeight;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        SongListViewController *songVC = [[SongListViewController alloc] init];
        MyMusicModel *model = [_tableArr objectAtIndex:indexPath.row];
        songVC.singerId = model.singer_id;
        songVC.imageUrl = model.pic_url;
        songVC.navigationItem.title = model.singer_name;
        [self.navigationController pushViewController:songVC animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络,请检查您的网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
}

-(void)startConnection
{
    [self createHud];
    NSString *url = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@&size=1000&page=1&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5&f=f10000&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=b550b825a9bbd677bcac4bdade7f055c02003702&idfa=63EE1A3C-4168-4037-B758-76D51C19FA56&utdid=Vbriu7dkO3MDAGL1gF8gdTOa&alf=10003483",self.styleId];
    [AsynConnect asynConnectWithUrlstr:url parmater:nil block:^(NSData *data) {
        NSMutableDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"rootDic = %@",rootDic);
        NSMutableArray *array = [rootDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            MyMusicModel *model = [[MyMusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_tableArr addObject:model];
            
            [model release];
        } NSLog(@"count = %ld",_tableArr.count);
        [_table reloadData];
        [_hud hide:YES];
    }];
    
}

//菊花效果
-(void)createHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.mode = MBProgressHUDModeIndeterminate;
    self.hud.labelText = @"加载中...";
    [self.hud show:YES];
    [self.view addSubview:_hud];
    [self.hud hide:YES afterDelay:5];
    [_hud release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
